package com.jdrx.ocp.beans.entity.util;

import java.util.regex.Pattern;

/**
 * @Author: Huangxg
 * @Date: 2019/4/24 13:35
 */
public class UrlUtils {

    static Pattern pattern = Pattern.compile("^http://.*?#.*?");

    /**
     * 根据url提取路由地址
     * @param url
     * @return
     */
    public static String getRouter(String url){
        if(pattern.matcher(url).matches()){
            return trim(url.substring(url.indexOf("#") + 1).replaceAll("/","."),'.');
        }
        return url;
    }

    /**
     * 去掉字符串两端指定的字符
     * @param value
     * @param filter
     * @return
     */
    public static String trim(String value,char filter){
        int len = value.length();
        int st = 0;
        char[] val = value.toCharArray();
        while ((st < len) && (val[st] <= filter)) {
            st++;
        }
        while ((st < len) && (val[len - 1] <= filter)) {
            len--;
        }
        return ((st > 0) || (len < value.length())) ? value.substring(st, len) : value;
    }

}
